package com.pokermind.dse.controller;

import com.pokermind.common.web.ApiResponse;
import com.pokermind.model.enums.ErrorCode;
import org.springframework.http.ResponseEntity;

/**
 * Base Controller Class
 * Provides unified response encapsulation methods to ensure consistent API response format
 * 
 * @author PokerMind Team
 */
public abstract class BaseController {

    // === Success Response Encapsulation Methods ===
    
    /**
     * Create success response
     */
    protected <T> ResponseEntity<ApiResponse<T>> success(T data) {
        return ResponseEntity.ok(ApiResponse.success(data));
    }
    
    /**
     * Create success response (no data)
     */
    protected ResponseEntity<ApiResponse<Void>> success() {
        return ResponseEntity.ok(ApiResponse.success());
    }
    
    // === Hand Business Specific Response Methods ===
    
    /**
     * Hand creation success response - HTTP 200
     */
    protected <T> ResponseEntity<ApiResponse<T>> handCreated(T data) {
        return ResponseEntity.status(200).body(ApiResponse.handCreated(data));
    }
    
    /**
     * Hand operation success response
     */
    protected <T> ResponseEntity<ApiResponse<T>> handOperationSuccess(T data, String operation) {
        return ResponseEntity.ok(ApiResponse.handOperationSuccess(data, operation));
    }
    
    // === AI Analysis Specific Response Methods ===
    
    /**
     * AI analysis success response
     */
    protected <T> ResponseEntity<ApiResponse<T>> aiAnalysisSuccess(T data) {
        return ResponseEntity.ok(ApiResponse.aiAnalysisSuccess(data));
    }
    
    // === Error Response Encapsulation Methods ===
    
    /**
     * Create error response using error code
     */
    protected <T> ResponseEntity<ApiResponse<T>> error(ErrorCode errorCode) {
        return ResponseEntity.status(errorCode.getHttpStatus())
                .body(ApiResponse.error(errorCode.getCode(), errorCode.getMessage()));
    }
    
    /**
     * Create error response using error code and custom message
     */
    protected <T> ResponseEntity<ApiResponse<T>> error(ErrorCode errorCode, String details) {
        // 将details合并到message中，或者直接使用errorCode的message
        String combinedMessage = details != null ? errorCode.getMessage() + ": " + details : errorCode.getMessage();
        return ResponseEntity.status(errorCode.getHttpStatus())
                .body(ApiResponse.error(errorCode.getCode(), combinedMessage));
    }
    
    /**
     * Create parameter validation error response
     */
    protected <T> ResponseEntity<ApiResponse<T>> validationError(String message) {
        return error(ErrorCode.VALIDATION_FAILED, message);
    }
    
    /**
     * Create 400 error response
     */
    protected <T> ResponseEntity<ApiResponse<T>> badRequest(ErrorCode errorCode) {
        return ResponseEntity.badRequest().body(ApiResponse.error(errorCode.getCode(), errorCode.getMessage()));
    }
    
    /**
     * Create 404 error response
     */
    protected <T> ResponseEntity<ApiResponse<T>> notFound(ErrorCode errorCode) {
        return ResponseEntity.notFound().build();
    }
    
    /**
     * Create 500 error response
     */
    protected <T> ResponseEntity<ApiResponse<T>> internalServerError(ErrorCode errorCode) {
        return ResponseEntity.status(500).body(ApiResponse.error(errorCode.getCode(), errorCode.getMessage()));
    }
    
    /**
     * Create 503 service unavailable error response
     */
    protected <T> ResponseEntity<ApiResponse<T>> serviceUnavailable(ErrorCode errorCode) {
        return ResponseEntity.status(503).body(ApiResponse.error(errorCode.getCode(), errorCode.getMessage()));
    }
}